<template>
  <div>
    <!-- tabs标签 -->
    <div class="form-row">
      <el-row>
        <el-tabs v-model="activeName">
          <el-tab-pane label="车辆信息" name="baseInfo">
            <baseInfo ref="baseInfo" :resData="this.resData" :type="this.type" />
          </el-tab-pane>
          <el-tab-pane label="保险保修" name="insurance">
            <insurance ref="insurance" :resData="this.resData" :type="this.type" />
          </el-tab-pane>
          <el-tab-pane label="会员信息" name="memberInfo">
            <memberInfo ref="memberInfo" :resData="this.resData" :type="this.type" />
          </el-tab-pane>
          <el-tab-pane label="车辆维修信息" name="vehicleMaintenance">
            <vehicleMaintenance ref="vehicleMaintenance" :resData="this.resData" />
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <AjaxButton v-if="type !== 'show'" :click="saveDrawer" type="primary">保 存</AjaxButton>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import baseInfo from './tabs/baseInfo.vue';
import vehicleRemark from './tabs/vehicleRemark.vue';
import insurance from './tabs/insurance.vue';
import memberInfo from './tabs/memberInfo.vue';
import vehicleMaintenance from './tabs/vehicleMaintenance/index.vue';
import { confirmAction, notifyAction, isFormEmpty } from '@/utils';
import Repairhistory from '@/pages/repair/business/customer/Repairhistory/historypop.vue';
import {
  getOwnerInfo,
  addVehicle,
  updateVehicle,
  getVehicleInfoByVinAndDealerCode,
  updateRepairWhenCustomerChange,
  getUnderRepairRecordCount
} from '@/api/customerRepair/customerInfo/ownerVehicleManagement';

export default {
  name: 'addVehiclePanel',
  components: {
    baseInfo,
    vehicleRemark,
    memberInfo,
    insurance,
    Repairhistory,
    vehicleMaintenance
  },
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    resData: {
      type: Object,
      default: {}
    },
    historyPop: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = {};
    return {
      activeName: 'baseInfo',
      vehicleInfo: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  watch: {},

  created() {},
  methods: {
    // handleChange(val) {
    //   this.$refs.insurance.newDate = val;
    // },
    async saveDrawer() {
      const data = await this.$refs.baseInfo.getData();
      const insuranceData = await this.$refs.insurance.getData();
      console.log('车辆保存');
      console.log(data);
      const res = await addVehicle({ ...data, ...insuranceData });
      if (res.resultCode === 200) {
        if (res.errMsg) {
          return notifyAction(res.errMsg, 'error');
        } else {
          notifyAction('保存成功！');
          console.log('保存成功');
          this.$emit('close', true);
        }
      }
      // const data = await this.$refs.baseInfo.getData();
      // const ownerNo = this.$refs.baseInfo.ownerNo;
      // const isChange = this.$refs.baseInfo.isChange;
      // if (isFormEmpty(data)) {
      //   await confirmAction('请填写必填项后再保存！');
      //   return;
      // }
      // let res = {};
      // let underRepairRes = {};
      // if (this.type === 'add') {
      //   res = await addVehicle({ ...data, distinguish: 'ZJ', ownerNo: ownerNo });
      // }
      // if (this.type === 'edit') {
      //   // 如果客户信息改变，需要提示客户是否更新在修委托书信息
      //   if (isChange) {
      //     // 查询车辆是否关联了在修的委托书信息
      //     underRepairRes = await getUnderRepairRecordCount({ vin: data.vin });
      //     if (underRepairRes.resultCode === 200 && underRepairRes.data > 0) {
      //       try {
      //         await confirmAction('是否更新当前在修状态委托书客户信息');
      //         await updateRepairWhenCustomerChange({ ...data, ownerNo: ownerNo });
      //       } catch (err) {
      //         console.log('未更新工单信息');
      //       }
      //     }
      //   }
      //   res = await updateVehicle(this.$refs.baseInfo.itemId, { ...data, ownerNo: ownerNo });
      // }
      // if (res.resultCode != 200) {
      //   // 因为字典的原因,保存页面成功返回码
      //   this.$message({
      //     type: 'error',
      //     message: '保存出错!'
      //   });
      // } else {
      //   this.$notify({
      //     title: '提示信息',
      //     message: '保存成功！',
      //     type: 'success'
      //   });
      //   // 页面刷新
      //   this.$emit('close', false, 'refresh');
      // }
    },
    closeDrawer() {
      console.log(this.$refs.baseInfo, 'baseInfo');
      this.$emit('close', false);
    }
  }
};
</script>

<style lang="less" scoped></style>
